package com.example.fragmentbrowser

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter

class TabsPagerAdapter(activity: FragmentActivity) : FragmentStateAdapter(activity) {
    private val fragmentsList = ArrayList<Fragment>()

    init {
        // 初始添加一个Fragment
        fragmentsList.add(MyFragment.newInstance(1))
    }

    fun addFragment(fragment: Fragment) {
        if (fragmentsList.size < 3) { // 确保最多添加三个Fragment
            fragmentsList.add(fragment)
            notifyItemInserted(fragmentsList.size - 1)
        }
    }

    override fun getItemCount(): Int = fragmentsList.size

    override fun createFragment(position: Int): Fragment = fragmentsList[position]
}